<?php

/**
 * Class Base_Controller
 * @author sunnyw
 */
class Base_Controller extends CI_Controller
{
    /**
     * @var array
     * ajax return msg
     */
    protected $ajax_msg;

    /**
     * @var array
     * current view data
     */
    protected $view_data = array();

    /**
     * template engine, smarty, twig ..
     * @var null
     */
    protected $tpl = null;

    function __construct()
    {
        parent::__construct();
        $this->ajax_msg = array(
            'info'    => '',
            'result'  => array(),
            'success' => false,
            'status'=>''
        );

        $this->set_tpl_engine('twig');

//        $this->pre_view_vars();
    }

    /**
     * setup smarty engine, default engine is twig
     * @param string $engine
     * @return null
     */
    protected function set_tpl_engine($engine = 'twig')
    {
        if(!in_array(strtolower($engine), ['twig','smarty']) ) {
            die('unsupported template engine: '.$engine);
        }

        if($engine == 'twig') {
            $this->load->library('Twig');
            $this->tpl = $this->twig;
            unset($this->twig);
        }

        if($engine == 'smarty') {
            $this->load->library('CI_Smarty');
            $this->tpl = $this->ci_smarty;
            unset($this->ci_smarty);
        }

        if(!isset($this->tpl)) {
            die('template engine init failed');
        }

        return $this->tpl;
    }

    //预先加载的全局views变量
//    function pre_view_vars()
//    {
//        //site keyword, title, description
//        $this->load->model('global_config_model','global_conf');
//    }

    /**
     * fetch view data
     * @param bool $key
     * @return array
     */
    public function get_view_data($key = false)
    {
        if($key === false) {
            return $this->view_data;
        }

        return $this->view_data[$key];
    }

    /**
     * save view data
     */
    public function set_view_data()
    {
        $args = func_get_args();
        if(count($args) == 1 && is_array($args[0])) {
            foreach($args[0] as $key => $val) {
                $this->view_data[$key] = $val;
            }
        }

        if(count($args) == 2 && is_string($args[0])) {
            $this->view_data[$args[0]] = $args[1];
        }
    }

    public function get_msg()
    {
        return $this->ajax_msg;
    }

    public function set_msg($opt = array())
    {
        $args = func_get_args();
        if(count($args) == 1 && is_array($args[0])) {
            foreach($args[0] as $key => $val) {
                if(array_key_exists($key, $this->ajax_msg)) {
                    $this->ajax_msg[$key] = $val;
                }
            }
        }

        if(count($args) == 2 && is_string($args[0])) {
            if(array_key_exists($args[0], $this->ajax_msg)) {
                $this->ajax_msg[$args[0]] = $args[1];
            }
        }

        return $this;
    }

    /**
     * return ajax message
     * @param array $opt
     */
    protected function put_msg($opt = array())
    {
        if(is_array($opt) && !empty($opt)) {
            foreach($opt as $name => $val) {
                if(array_key_exists($name, $this->ajax_msg)) {
                    $this->ajax_msg[$name] = $val;
                }
            }
        }

        header('Content-Type:application/json; charset=utf-8');
        echo json_encode($this->ajax_msg);
        exit;
    }

}

/**
 * 前台控制器基类
 * Class Home_Controller
 */
class Home_Controller extends Base_Controller
{
//    protected $uri_segment;

    function __construct()
    {
        parent::__construct();

        $this->check_login();

//        $this->load->library('PageBuilder');
//        $sidebar = $this->pagebuilder->side_menu();
//        $this->set_view_data(array(
//            'side_menu' => $sidebar
//        ));

//        $this->uri_segment=$this->uri->rsegment_array();
    }


    /**
     * 检查登录状态
     */
    protected function check_login()
    {
        if( !$this->is_login() ) {
            redirect(base_url() . 'login');
        }
        else {
            $rediret_url = $this->input->get('redirect', true);

            if(!empty($rediret_url)) {
                redirect(urlencode($rediret_url));
            }

            return true;
        }
    }

    protected function is_login()
    {
        return null !== $this->session->userdata('username');
    }

}
